home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Ident.php < prev    next >
PHP Script  |  2004-03-24  |  11KB  |  334 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Author:          Ondrej Jombik <nepto@pobox.sk>                      |
  18. // | Original author: Gavin Brown <gavin.brown@uk.com>                    |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Ident.php,v 1.1 2002/10/03 20:40:42 nepto Exp $
  22. //
  23. // Identification Protocol implementation
  24. //
  25.  
  26. require_once 'PEAR.php';
  27.  
  28. /**
  29.  * Net_Ident default values
  30.  * @const   NET_IDENT_DEFAULT_TIMEOUT   Default ident query network timeout
  31.  * @const   NET_IDENT_DEFAULT_PORT      Default ident protocol port
  32.  */
  33. define('NET_IDENT_DEFAULT_TIMEOUT',      30);
  34. define('NET_IDENT_DEFAULT_PORT',        113);
  35.  
  36. /**
  37.  * Net_Ident object states
  38.  * @const   NET_IDENT_STATUS_UNDEF      Undefined Net_Ident object state
  39.  * @const   NET_IDENT_STATUS_OK         Net_Ident object did successful query
  40.  * @const   NET_IDENT_STATUS_ERROR      Net_Ident object query failed
  41.  */
  42. define('NET_IDENT_STATUS_UNDEF',          0);
  43. define('NET_IDENT_STATUS_OK',             1);
  44. define('NET_IDENT_STATUS_ERROR',          2);
  45.  
  46. /**
  47.  * Net_Ident - Identification Protocol implementation according to RFC 1413
  48.  *
  49.  * The Identification Protocol (a.k.a., "ident", a.k.a., "the Ident Protocol")
  50.  * provides a means to determine the identity of a user of a particular TCP
  51.  * connection.  Given a TCP port number pair, it returns a character string
  52.  * which identifies the owner of that connection on the server's system.
  53.  *
  54.  * You can find out more about the Ident protocol at
  55.  *
  56.  *      http://www.ietf.org/rfc/rfc1413.txt
  57.  *
  58.  * Usage:
  59.  *   <?php
  60.  *       require_once 'Net/Ident.php';
  61.  *       $ident   = new Net_Ident;
  62.  *       $user    = $ident->getUser();
  63.  *       $os_type = $ident->getOsType();
  64.  *       echo "user: $user, operating system: $os_type\n";
  65.  *   ?>
  66.  *
  67.  * @author      Ondrej Jombik <nepto@pobox.sk>
  68.  * @package     Net_Ident
  69.  * @version     1.0
  70.  * @access      public
  71.  */
  72. class Net_Ident
  73. {
  74.     /**
  75.      * Current object state (undef, ok, error)
  76.      *
  77.      * @var     enum
  78.      * @access  private
  79.      */
  80.     var $_status;
  81.  
  82.     /**
  83.      * Error message string;
  84.      *   if $_status is "error", $_error contains system error message;
  85.      *   if $_status is "ok", $_error contains ident error message
  86.      * or it is empty
  87.      *
  88.      * @var     string
  89.      * @access  private
  90.      */
  91.     var $_error;
  92.  
  93.     /**
  94.      * Properties array (contains remote host, remote port, ident port, etc.)
  95.      *
  96.      * @var     array
  97.      * @access  private
  98.      */
  99.     var $_props;
  100.  
  101.     /**
  102.      * Data array (contains ident username, ident operating system type, and
  103.      * raw line returned from ident server)
  104.      *
  105.      * @var     array
  106.      * @access  private
  107.      */
  108.     var $_data;
  109.  
  110.     /**
  111.      * Net_Ident object constructor
  112.      *
  113.      * Initializes class properties. Use empty string '' for any string
  114.      * parameter and value of 0 for any int parameter to set default value.
  115.      *
  116.      * @param   string  $remote_addr    Remote host address (IP or hostname)
  117.      * @param   int     $remote_port    Remote port (default $REMOTE_PORT)
  118.      * @param   int     $local_port     Local port  (default $SERVER_PORT)
  119.      * @param   int     $ident_port     Ident port  (default 113)
  120.      * @param   int     $timeout        Socket timeout (default 30 seconds)
  121.      * @return  none
  122.      * @access  public
  123.      */
  124.     function Net_Ident(
  125.             $remote_addr = '',
  126.             $remote_port = 0,
  127.             $local_port  = 0,
  128.             $ident_port  = 0,
  129.             $timeout     = 0)
  130.     {
  131.         $this->_status = NET_IDENT_STATUS_UNDEF;
  132.         $this->setRemoteAddr($remote_addr);
  133.         $this->setRemotePort($remote_port);
  134.         $this->setLocalPort($local_port);
  135.         $this->setIdentPort($ident_port);
  136.         $this->setTimeout($timeout);
  137.     }
  138.  
  139.     /**
  140.      * Sets remote host address (IP or hostname)
  141.      *
  142.      * @param   string  $remote_addr    Remote host address (IP or hostname)
  143.      * @return  none
  144.      * @access  public
  145.      */
  146.     function setRemoteAddr($remote_addr)
  147.     {
  148.         strlen($remote_addr) <= 0 && $remote_addr = $_SERVER['REMOTE_ADDR'];
  149.         $this->_props['remote_addr'] = $remote_addr;
  150.     }
  151.  
  152.     /**
  153.      * Sets remote port
  154.      *
  155.      * @param   int     $remote_port    Remote port (default $REMOTE_PORT)
  156.      * @return  none
  157.      * @access  public
  158.      */
  159.     function setRemotePort($remote_port)
  160.     {
  161.         $remote_port = intval($remote_port);
  162.         $remote_port <= 0 && $remote_port = $_SERVER['REMOTE_PORT'];
  163.         $this->_props['remote_port'] = $remote_port;
  164.     }
  165.  
  166.     /**
  167.      * Sets local port
  168.      *
  169.      * @param   int     $local_port     Local port  (default $SERVER_PORT)
  170.      * @return  none
  171.      * @access  public
  172.      */
  173.     function setLocalPort($local_port)
  174.     {
  175.         $local_port = intval($local_port);
  176.         $local_port <= 0 && $local_port = $_SERVER['SERVER_PORT'];
  177.         $this->_props['local_port'] = $local_port;
  178.     }
  179.  
  180.     /**
  181.      * Sets ident port
  182.      *
  183.      * @param   int     $ident_port     Ident port  (default 113)
  184.      * @return  none
  185.      * @access  public
  186.      */
  187.     function setIdentPort($ident_port)
  188.     {
  189.         $ident_port = intval($ident_port);
  190.         $ident_port <= 0 && $ident_port = NET_IDENT_DEFAULT_PORT;
  191.         $this->_props['ident_port'] = $ident_port;
  192.     }
  193.  
  194.     /**
  195.      * Sets socket timeout
  196.      *
  197.      * @param   int     $timeout        Socket timeout (default 30 seconds)
  198.      * @return  none
  199.      * @access  public
  200.      */
  201.     function setTimeout($timeout)
  202.     {
  203.         $timeout = intval($timeout);
  204.         $timeout <= 0 && $timeout = NET_IDENT_DEFAULT_TIMEOUT;
  205.         $this->_props['timeout'] = $timeout;
  206.     }
  207.  
  208.     /**
  209.      * Performs network socket ident query
  210.      *
  211.      * @return  mixed   PEAR_Error on connection error
  212.      *                  rawdata read from socket on success
  213.      * @access  public
  214.      */
  215.     function query()
  216.     {
  217.         // query forced, clean current result
  218.         if ($this->_status == NET_IDENT_STATUS_OK) {
  219.             unset($this->_data['username']);
  220.             unset($this->_data['os_type']);
  221.             $this->_status = NET_IDENT_STATUS_UNDEF;
  222.         }
  223.         while (1) {
  224.             if ($this->_status == NET_IDENT_STATUS_ERROR) {
  225.                 return new PEAR_Error($this->_error);
  226.             }
  227.  
  228.             if ($socket = @fsockopen(
  229.                         $this->_props['remote_addr'],
  230.                         $this->_props['ident_port'],
  231.                         $errno, $errstr,
  232.                         $this->_props['timeout'])) {
  233.                 break;
  234.             }
  235.             $this->_status = NET_IDENT_STATUS_ERROR;
  236.             $this->_error  = 'Error connecting to ident server ('
  237.                     .$this->_props['remote_addr'].':'
  238.                     .$this->_props['ident_port']."): $errstr ($errno)"; // )
  239.         }
  240.  
  241.         $line = $this->_props['remote_port'].','
  242.             .$this->_props['local_port']."\r\n";
  243.         @fwrite($socket, $line);
  244.         $line = @fgets($socket, 1000); // 1000 octets according to RFC 1413
  245.         fclose($socket);
  246.  
  247.         $this->_status = NET_IDENT_STATUS_OK;
  248.         $this->_data['rawdata'] = $line;
  249.         $this->_parseIdentReponse($line);
  250.  
  251.         return $line;
  252.     }
  253.  
  254.     /**
  255.      * Returns ident username
  256.      *
  257.      * @return  mixed   PEAR_Error on connection error
  258.      *                  false boolean on ident protocol error
  259.      *                  username string on success
  260.      * @access  public
  261.      */
  262.     function getUser()
  263.     {
  264.         $this->_status == NET_IDENT_STATUS_UNDEF && $this->query();
  265.         if ($this->_status == NET_IDENT_STATUS_ERROR) {
  266.             return new PEAR_Error($this->_error);
  267.         }
  268.         return $this->_data['username'];
  269.     }
  270.  
  271.     /**
  272.      * Returns ident operating system type
  273.      *
  274.      * @return  mixed   PEAR_Error on connection error
  275.      *                  false boolean on ident protocol error
  276.      *                  operating system type string on success
  277.      * @access  public
  278.      */
  279.     function getOsType()
  280.     {
  281.         $this->_status == NET_IDENT_STATUS_UNDEF && $this->query();
  282.         if ($this->_status == NET_IDENT_STATUS_ERROR) {
  283.             return new PEAR_Error($this->_error);
  284.         }
  285.         return $this->_data['os_type'];
  286.     }
  287.  
  288.     /**
  289.      * Returns ident protocol error
  290.      *
  291.      * @return  mixed   error string if ident protocol error had occured
  292.      *                  false otherwise
  293.      * @access  public
  294.      */
  295.     function identError()
  296.     {
  297.         if ($this->_status == NET_IDENT_STATUS_OK
  298.                 && isset($this->_error)) {
  299.             return $this->_error;
  300.         }
  301.         return false;
  302.     }
  303.  
  304.     /**
  305.      * Parses response from indent server and sets internal data structures
  306.      * with ident username and ident operating system type
  307.      *
  308.      * @param   string  $string     ident server response
  309.      * @return  boolean true if no ident protocol error had occured
  310.      *                  false otherwise
  311.      * @access  private
  312.      */
  313.     function _parseIdentReponse($string)
  314.     {
  315.         list(, $response)           = explode(':', $string, 2);
  316.         list($resp_type, $add_info) = explode(':', trim($response), 2);
  317.         if (trim($resp_type) == 'USERID') {
  318.             list($os_type, $username) = explode(':', trim($add_info), 2);
  319.             $this->_data['username']  = trim($username);
  320.             $this->_data['os_type']   = trim($os_type);
  321.             return true;
  322.         } elseif (trim($resp_type) == 'ERROR') {
  323.             $this->_error = trim($add_info);
  324.         } else {
  325.             $this->_error = 'Invalid ident server response';
  326.         }
  327.         $this->_data['username'] = false;
  328.         $this->_data['os_type']  = false;
  329.         return false;
  330.     }
  331. }
  332.  
  333. ?>
  334.